package com.iscas.tech.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iscas.tech.entity.AtBasemodules;
import com.iscas.tech.mapper.AtBasemodulesMapper;
import com.iscas.tech.param.AtBasemodulesPageParam;
import com.iscas.tech.service.AtBasemodulesService;
import com.iscas.tech.vo.AtBasemodulesQueryVo;
import com.iscas.tech.vo.MdAndMvQueryVo;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.pagination.PageUtil;
import io.geekidea.springbootplus.framework.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目基础信息模块表 服务实现类
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@Service
public class AtBasemodulesServiceImpl extends BaseServiceImpl<AtBasemodulesMapper, AtBasemodules> implements AtBasemodulesService {

    @Autowired
    private AtBasemodulesMapper atBasemodulesMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveAtBasemodules(AtBasemodules atBasemodules) throws Exception {
        Integer sort = atBasemodulesMapper.getMaxMdSort(atBasemodules.getMdType());
        if (sort == null) {
            sort = 0;
        }
        atBasemodules.setMdSort(sort + 1);
        return super.save(atBasemodules);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAtBasemodules(AtBasemodules atBasemodules) throws Exception {
        return super.updateById(atBasemodules);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteAtBasemodules(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public Paging<AtBasemodulesQueryVo> getAtBasemodulesPageList(AtBasemodulesPageParam atBasemodulesPageParam) throws Exception {
        Page page = PageUtil.getPage(atBasemodulesPageParam, OrderItem.desc(getLambdaColumn(AtBasemodules::getCreatetime)));
        IPage<AtBasemodulesQueryVo> iPage = atBasemodulesMapper.getAtBasemodulesPageList(page, atBasemodulesPageParam);
        return new Paging(iPage);
    }

    @Override
    public List<AtBasemodulesQueryVo> getAll(Integer mdType) throws Exception {
        return atBasemodulesMapper.getAll(mdType);
    }

    @Override
    public AtBasemodulesQueryVo get(Long id) throws Exception {
        return atBasemodulesMapper.get(id);
    }

    @Override
    public List<MdAndMvQueryVo> getDetailAll(Integer mdType) throws Exception {
        return atBasemodulesMapper.getDetailAll(mdType);
    }

}
